<?php
	include '../config.php';
	include '../includes/user.class.php';
	$isloggedin = CheckLogin($mysql);
	$perms = CheckPermissions($mysql);
	if($perms < 2){
		die('permissionerror');
	}
	
	include '../includes/blog.php';
	$blog = new Blog();
	
	if(!empty($_POST['np'])){
		// Add new post
		$title = $_POST['title'];
		$content = $_POST['content'];
		$author = $_COOKIE['username'];
		$blog->NewEntry($mysql,$title,$content,$author);
		echo $title . " successfully added!";
	}elseif(!empty($_POST['ep'])){
		// Get post content for editing
		$id = $_POST['id'];
		$blog->AdminSingle($mysql,$id);
	}elseif(!empty($_POST['dp'])){
		// delete a post
		$id = $_POST['id'];
		$blog->DeleteEntry($mysql,$id);
		echo "Blog post deleted.";
	}elseif(!empty($_POST['sp'])){
		// save an old post edit
		$id = $_POST['id'];
		$title = $_POST['title'];
		$content = $_POST['content'];
		$blog->SaveEntry($mysql,$id,$title,$content);
		echo $title . " updated.";
	}elseif(!empty($_POST['al'])){
		// navigate the thread list
		$offset = $_POST['offset'];
		$blog->AdminList($mysql,$offset);
	}
?>